//
//  AppDelegate.swift
//  weiBo
//
//  Created by helong on 16/7/9.
//  Copyright © 2016年 helong. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    var notiObject: NSObjectProtocol?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions:
         //window监听通知,决定跟控制器是谁
        [NSObject: AnyObject]?) -> Bool {
            
            window = UIWindow()
        
        /**
        *  根据通知决定跳转到哪个控制器
        */
       notiObject = NSNotificationCenter.defaultCenter().addObserverForName(switchControllerNotificationName, object: nil, queue: nil) { (noti) -> Void in
            //接收到通知之后要做的事情
            self.switchController(noti)
        }
        
        let rootVC = HRMainTabBarController()
        window?.rootViewController = rootVC
        window?.makeKeyAndVisible()

        return true
    }

     // MARK: - 跳转哪个控制器
    func switchController(noti: NSNotification){

        let object = noti.object
        
        printLog()
        if object is HRLoginPage {//如果是从访客控制器发来的通知,根控制器为欢迎页面控制器
            window?.rootViewController = HRWelcomeViewController()
        }else{//如果不是从访客控制发来的就是从欢迎页面发过来的那么跳到主页控制器
            window?.rootViewController = HRMainTabBarController()
        }
        
    }
     // MARK: - 移除通知
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(notiObject!)
    }
    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        //  删除7天前的数据
        CZStatusDAL.clearCacheData()
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

